package txc.test.web.controller;

import com.taobao.txc.client.aop.annotation.TxcTransaction;
import com.taobao.txc.common.TxcContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import txc.test.model.T;
import txc.test.service.TService;
import txc.test.service.api1.TServiceApi1;
import txc.test.service.api2.TServiceApi2;
import txc.test.web.util.ResultData;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;



@Controller
public class HelloController {

	@Autowired
	private TService tService;

	@Autowired
	private TServiceApi1 tServiceApi1;


	@Autowired
	private TServiceApi2 tServiceApi2;
	


	@RequestMapping("/")
	public String index(Map<String, Object> model) {
		return "/index";
	}

	@RequestMapping("/normal")
	@ResponseBody
	public ResultData normal(Map<String, Object> model) {
		Map map = new HashMap();
		Random r = new Random(new Date().getTime());
		txc.test.dto1.TDTO dto1 = new txc.test.dto1.TDTO();
		dto1.setValue(r.nextInt(Integer.MAX_VALUE));
		dto1.setCreateTime(new Date());
		txc.test.dto2.TDTO dto2 = new txc.test.dto2.TDTO();
		dto2.setValue(r.nextInt(Integer.MAX_VALUE));
		dto2.setCreateTime(new Date());
		tServiceApi1.insertSelective(dto1);
		tServiceApi2.insertSelective(dto2);

		T t = new T();
		t.setCreateTime(new Date());
		t.setValue(r.nextInt(Integer.MAX_VALUE));
		tService.insertSelective(t);

		map.put("status","success");
		System.out.println("normal invoked!");
		return success().setMap(map);
	}

	@RequestMapping("/txcTest")
	@ResponseBody
	public ResultData txcTest(Map<String, Object> model,HttpServletRequest request){
		WebApplicationContext springContext = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());
		HelloController bean = (HelloController)springContext.getBean("helloController");
		bean.doTxcTest();
		Map map = new HashMap();
		map.put("status","error");
		System.out.println("txcTest invoked!");
		return success().setMap(map);
	}

	@TxcTransaction(appName = "myTxcTest")
	public void doTxcTest(){
		String txcId = TxcContext.getCurrentXid();
		Random r = new Random(new Date().getTime());
		txc.test.dto1.TDTO dto1 = new txc.test.dto1.TDTO();
		dto1.setValue(r.nextInt(Integer.MAX_VALUE));
		dto1.setCreateTime(new Date());
		txc.test.dto2.TDTO dto2 = new txc.test.dto2.TDTO();
		dto2.setValue(r.nextInt(Integer.MAX_VALUE));
		dto2.setCreateTime(new Date());
		tServiceApi1.insertSelectiveTxc(dto1,txcId);
		tServiceApi2.insertSelectiveTxc(dto2,txcId);

		T t = new T();
		t.setCreateTime(new Date());
		t.setValue(r.nextInt(Integer.MAX_VALUE));
		tService.insertSelective(t);

		throw new RuntimeException("求回滚");
	}


	private ResultData fail() {
		return new ResultData().setSuccess(false).setCode(500);
	}

	private ResultData success() {
		return new ResultData().setSuccess(true).setCode(200);
	}








}
